﻿using System;
using Kugar.Core.Configuration;
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons;
using Kugar.MVC.MobileUI.WeUI.PageControls.PageForm;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace Kugar.MVC.MobileUI.WeUI   
{
    public class MobileWeUI: IViewContextAware
    {
        private IHtmlHelper _htmlhelper = null;
        private static string _resourceUrl = "";
        private static bool _isDebugModel = false;
        private ViewContext _viewContext = null;
        private int _nextControlIndex = 0;
        private Lazy<PageCommonCtrl> _ctrl = null;
        private Lazy<SystemComponent> _systemComponent ;
        private Lazy<OtherJsPluginStatus> _otherJsPlugin = null;


        static MobileWeUI()
        {
            _resourceUrl = CustomConfigManager.Default.AppSettings.GetValueByName<string>("WebUI:ResourceUrl", "");

            if (string.IsNullOrWhiteSpace(_resourceUrl))
            {
                _resourceUrl = "/mobileweui";
            }

            _isDebugModel =
                CustomConfigManager.Default.AppSettings.GetValueByName<string>("WebUI:DebugModel", "false")
                    .ToBool(false);
        }

        internal MobileWeUI(IHtmlHelper htmlHelper)
        {
            _htmlhelper = htmlHelper;

            _ctrl = new Lazy<PageCommonCtrl>(createControl);

            _systemComponent = new Lazy<SystemComponent>(createSys);

            _otherJsPlugin = new Lazy<OtherJsPluginStatus>(()=>new OtherJsPluginStatus(ViewContext));
        }

        /// <summary>
        /// AdmninUI组件相关的一些路径
        /// </summary>
        public static LayoutPath LayoutPath => new LayoutPath();


        public int NextControlID()
        {
            _nextControlIndex += 1;

            return _nextControlIndex;
        }

        public string Title
        {
            get { return ((string)_viewContext.ViewBag.Title).ToStringEx(); }
            set { _viewContext.ViewBag.Title = value.ToStringEx(); }
        }

        public bool IsDebugModel => _isDebugModel;

        public bool IsInModal
        {
            get { return ((bool?)ViewContext.ViewBag.IsInModal).GetValueOrDefault(false); }
        }

        public bool IsInForm
        {
            get
            {
                var count = (int)ViewContext.TempData.TryGetValue("FormCount", 0);

                return count > 0;
            }
        }

        public PageForm_Form Form() => new PageForm_Form(_viewContext);

        //public AuthorityValidator AuthorityValidator { set; get; } = EmptyAuthorityValidator.Empty;

        public PageCommonCtrl CommonCtrl() => _ctrl.Value;

        public SystemComponent SystemComponent() => _systemComponent.Value;

        public IHtmlHelper HtmlHelper => _htmlhelper;

        public ViewContext ViewContext => _htmlhelper.ViewContext;

        public class PageCommonCtrl
        {
            private MobileWeUI _weui = null;

            public PageCommonCtrl(MobileWeUI weui)
            {
                _weui = weui;
            }

            protected MobileWeUI WeUI => _weui;

            public PageCommon_Cells Cells() => new PageCommon_Cells(_weui._viewContext);

            /// <summary>
            /// 输出一个flex控件容器
            /// </summary>
            /// <returns></returns>
            public PageCommon_FlexLayoutContainer FlexLayoutContainer() => new PageCommon_FlexLayoutContainer(_weui._viewContext);

            /// <summary>
            /// 输出一个Flex控件,,非容器,不需要using
            /// </summary>
            /// <returns></returns>
            public PageCommon_FlexLayout FlexLayout() => new PageCommon_FlexLayout(_weui._viewContext);

            /// <summary>
            /// 用于输出九宫格
            /// </summary>
            /// <returns></returns>
            public PageCommon_GridLayout GridLayout() => new PageCommon_GridLayout(_weui._viewContext);

            //public PageCommon_List List()=>new PageCommon_List(_weui._viewContext);

            /// <summary>
            /// 输出一个loadmore控件,,建议不直接使用该控件,而是使用listview或者tabbar或者navbar等自带的loadmore功能
            /// </summary>
            /// <returns></returns>
            public PageCommon_LoadMore LoadMore() => new PageCommon_LoadMore(_weui._viewContext);

            /// <summary>
            /// 消息页面,可以使用 WebUIJS.ShowMsgPageByUrl 弹出,也可以直接在页面中输出
            /// </summary>
            /// <returns></returns>
            public PageCommon_MsgPage MsgPage() => new PageCommon_MsgPage(_weui._viewContext);

            /// <summary>
            /// 输出一个带标题的panel容器控件
            /// </summary>
            /// <returns></returns>
            public PageCommon_Panel Panel() => new PageCommon_Panel(_weui._viewContext);

            /// <summary>
            /// 图片轮播控件
            /// </summary>
            /// <returns></returns>
            public PageCommon_Swiper Swiper() => new PageCommon_Swiper(_weui._viewContext);

            /// <summary>
            /// 轮播控件,使用容器的方式输出轮播子项
            /// </summary>
            /// <returns></returns>
            public PageCommon_SwiperContainer SwiperContainer() => new PageCommon_SwiperContainer(_weui._viewContext);

            /// <summary>
            /// 输出一个TabBar,一般用于首页,按钮在底部
            /// </summary>
            /// <returns></returns>
            public PageCommon_TabBar TabBar() => new PageCommon_TabBar(_weui._viewContext);

            /// <summary>
            /// 输出导航栏,按钮栏在顶部
            /// </summary>
            /// <returns></returns>
            public PageCommon_NavBar NavBar() => new PageCommon_NavBar(_weui._viewContext);

            /// <summary>
            /// 输出一个模态弹出框的容器
            /// </summary>
            /// <returns></returns>
            public PageCommon_ModalWindow Modal() => new PageCommon_ModalWindow(_weui._viewContext);

            /// <summary>
            /// 输出一个listview容器控件,并允许定义每行的模板
            /// </summary>
            /// <typeparam name="TData"></typeparam>
            /// <returns></returns>
            public PageCommon_ListViewContainer<TData> ListView<TData>() => new PageCommon_ListViewContainer<TData>(_weui._viewContext);

            /// <summary>
            /// 输出一个底部元素
            /// </summary>
            /// <returns></returns>
            public PageCommon_Footer PageFooter() => new PageCommon_Footer(_weui._viewContext);


            public PageCommon_ListActionBar ListActionBar() => new PageCommon_ListActionBar(_weui._viewContext);

            public PageCommon_ListQueryBox ListQueryBox() => new PageCommon_ListQueryBox(_weui._viewContext);

            /// <summary>
            /// 输出一个footer底部的元素容器
            /// </summary>
            /// <returns></returns>
            public PageCommon_FooterContainer PageFooterContainer() => new PageCommon_FooterContainer(_weui._viewContext);

            /// <summary>
            /// 数据选择器
            /// </summary>
            /// <typeparam name="TData"></typeparam>
            /// <returns></returns>
            public PageCommon_DataPicker<TData> DataPicker<TData>() => new PageCommon_DataPicker<TData>(_weui._viewContext);

            public PageCommon_PopupWindow PopupWindow() => new PageCommon_PopupWindow(_weui._viewContext);
        }
        

        public OtherJsPluginStatus JsPluginStatus => _otherJsPlugin.Value;

        public void Contextualize(ViewContext viewContext)
        {
            if (_htmlhelper is IViewContextAware)
            {
                ((IViewContextAware)_htmlhelper).Contextualize(viewContext);

                ViewContext.ViewBag.WebUI = this;

                if (ViewContext.TempData.ContainsKey("nextControlIndex"))
                {
                    _nextControlIndex = (int)ViewContext.TempData["nextControlIndex"];
                }
                else
                {
                    ViewContext.TempData["nextControlIndex"] = _nextControlIndex;
                }
            }
        }

        private PageCommonCtrl createControl()
        {
            return new PageCommonCtrl(this);
        }

        private SystemComponent createSys()
        {
            return new SystemComponent(ViewContext);
        }
    }

    /// <summary>
    /// 第三方组件使用状态
    /// </summary>
    public class OtherJsPluginStatus
    {
        private ViewContext _viewContext;

        public OtherJsPluginStatus(ViewContext viewContext)
        {
            _viewContext = viewContext;

            //_viewContext.ViewBag.Swiper = false;
            //_viewContext.ViewBag.CityPicker = false;
        }

        public bool Swiper
        {
            get { return ((bool?)_viewContext.ViewBag.Swiper).GetValueOrDefault(false); }
        }

        public bool CityPicker
        {
            get { return ((bool?)_viewContext.ViewBag.CityPicker).GetValueOrDefault(false); }
        }
    }

    public class LayoutPath
    {
        internal LayoutPath() { }
        
        public string ResourceBaseUrl { set; get; } = "/weuibase";

        /// <summary>
        /// 返回MobileWeUILayout的路径
        /// </summary>
        public static string DefaultLayoutPath => "~/Views/Shared/MobileWeUILayout.cshtml";

    }

    public class SystemComponent
    {
        private ViewContext _viewContext = null;

        internal SystemComponent(ViewContext viewContext)
        {
            _viewContext = viewContext;
        }

        /// <summary>
        /// 输出服务器端的弹出框信息,只有在加载弹出页或者自定义页面的时候,才需要使用
        /// </summary>
        /// <returns></returns>
        public PageCommon_ServerMsg ServerMsg() => new PageCommon_ServerMsg(_viewContext);
    }
}
